package pub.tbc.toolkit.http2.template;

import com.alibaba.fastjson.JSON;
import pub.tbc.toolkit.Factory;
import pub.tbc.toolkit.http2.HttpContentType;
import pub.tbc.toolkit.http2.HttpRequesterImpl;
import pub.tbc.toolkit.http2.ResponseWrapper;

import java.util.Map;

/**
 * @author tbc on 2016/9/18 11:31:13.
 */
public class HttpTemplate {

    public static <T> T get(String url, Class<T> c) {
        return get(url, null, c);
    }

    public static <T> T get(String url, Map<String, Object> param, Class<T> c) {
        return get(url, null, param, c);
    }

    public static <T> T get(String url, Map<String, String> headers, Map<String, Object> param, Class<T> c) {
        ResponseWrapper response = new HttpRequesterImpl().get(url, headers, param);
        if (response.isOk())
            return (T) JSON.parseObject(response.getResult(), c);
        throw new RuntimeException("调用目标地址 " + url + " 失败：" + response.getHttpCode());
    }

    public static <T> T delete(String url, Class<T> c) {
        return delete(url, null, c);
    }

    public static <T> T delete(String url, Map<String, Object> param, Class<T> c) {
        return delete(url, null, param, c);
    }

    public static <T> T delete(String url, Map<String, String> headers, Map<String, Object> param, Class<T> c) {
        ResponseWrapper response = new HttpRequesterImpl().delete(url, headers, param);
        if (response.isOk())
            return (T) JSON.parseObject(response.getResult(), c);
        throw new RuntimeException("调用目标地址 " + url + " 失败：" + response.getHttpCode());
    }

    public static <T> T postForJson(String url, Map<String, Object> body, Class<T> c) {
        return post(
                url,
                Factory.asHashMap(HttpContentType.getName(), HttpContentType.APPLICATION_JSON.getMimeType()),
                body,
                c);
    }

    public static String postForJson(String url, Map<String, Object> body) {
        return post(url, Factory.asHashMap(HttpContentType.getName(), HttpContentType.APPLICATION_JSON.getMimeType()), body);
    }


    public static <T> T postForTextXml(String url, Map<String, Object> body, Class<T> c) {
        return post(
                url,
                Factory.asHashMap(HttpContentType.getName(), HttpContentType.TEXT_XML.getMimeType()),
                body,
                c);
    }

    public static String postForTextXml(String url, Map<String, Object> body) {
        return post(url, Factory.asHashMap(HttpContentType.getName(), HttpContentType.TEXT_XML.getMimeType()), body);
    }


    public static <T> T postForForm(String url, Map<String, Object> body, Class<T> c) {
        return post(
                url,
                Factory.asHashMap(HttpContentType.getName(), HttpContentType.CONTENT_TYPE_FORM.getMimeType()),
                body,
                c);
    }

    public static String postForForm(String url, Map<String, Object> body) {
        return post(url, Factory.asHashMap(HttpContentType.getName(), HttpContentType.CONTENT_TYPE_FORM.getMimeType()), body);
    }


    public static <T> T post(String url, Map<String, String> headers, Map<String, Object> param, Class<T> c) {
        ResponseWrapper response = new HttpRequesterImpl().post(url, headers, param);
        if (response.isOk())
            return (T) JSON.parseObject(response.getResult(), c);
        throw new RuntimeException("调用目标地址 " + url + " 失败：" + response.getHttpCode());
    }

    public static String post(String url, Map<String, String> headers, Map<String, Object> param) {
        ResponseWrapper response = new HttpRequesterImpl().post(url, headers, param);
        if (response.isOk())
            return response.getResult();
        throw new RuntimeException("调用目标地址 " + url + " 失败：" + response.getHttpCode());
    }


    public static <T> T putForJson(String url, Map<String, Object> body, Class<T> c) {
        return put(
                url,
                Factory.asHashMap(HttpContentType.getName(), HttpContentType.APPLICATION_JSON.getMimeType()),
                body,
                c);
    }

    public static <T> T putForTextXml(String url, Map<String, Object> body, Class<T> c) {
        return put(
                url,
                Factory.asHashMap(HttpContentType.getName(), HttpContentType.TEXT_XML.getMimeType()),
                body,
                c);
    }

    public static <T> T putForForm(String url, Map<String, Object> body, Class<T> c) {
        return put(
                url,
                Factory.asHashMap(HttpContentType.getName(), HttpContentType.CONTENT_TYPE_FORM.getMimeType()),
                body,
                c);
    }

    public static <T> T put(String url, Map<String, String> headers, Map<String, Object> param, Class<T> c) {
        ResponseWrapper response = new HttpRequesterImpl().post(url, headers, param);
        if (response.isOk())
            return (T) JSON.parseObject(response.getResult(), c);
        throw new RuntimeException("调用目标地址 " + url + " 失败：" + response.getHttpCode());
    }

}
